﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsMerge
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "文本文件(*.txt)|*.txt";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dlg.FileName;
            }
        }

        private void btnFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "文本文件(*.txt)|*.txt";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dlg.FileName;
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("文件1不能为空!");
                return;
            }

            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("文件2不能为空!");
                return;
            }

            FileInfo f = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
            DirectoryInfo dir = new DirectoryInfo(Path.Combine(f.DirectoryName, "Data"));
            if (!dir.Exists) dir.Create();

            //创建文件
            //读取文件1
            string file1 = null;
            using (StreamReader reader = new StreamReader(textBox1.Text))
            {
                file1 = reader.ReadToEnd();
            }
            string file2 = null;
            using (StreamReader reader = new StreamReader(textBox2.Text))
            {
                file2 = reader.ReadToEnd();
            }

            using (StreamWriter wr = new StreamWriter(Path.Combine(dir.FullName, "m.txt")))
            {
                wr.Write(file1);
                wr.Write(file2);
            }
            MessageBox.Show("合并完成");
        }
    }
}
